package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.egg.ylt.R;
import com.egg.ylt.pojo.HeatTitleEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/10/22.
 */

public class ADA_SearchHeat extends RecyclerView.Adapter<ADA_SearchHeat.HeatViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<HeatTitleEntity.ListBean> mList;
    private OnItemClickListener itemClickListener;

    public ADA_SearchHeat(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
        mInflater = LayoutInflater.from(mContext);
    }

    public void setmList(List<HeatTitleEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public HeatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HeatViewHolder(mInflater.inflate(R.layout.item_search_history, parent, false));
    }

    @Override
    public void onBindViewHolder(HeatViewHolder holder, int position) {
        final HeatTitleEntity.ListBean listBean = mList.get(position);
        holder.tvContent.setText(listBean.getKeyword());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onClick(listBean.getKeyword());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class HeatViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_content)
        TextView tvContent;
        public HeatViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(String id);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
